"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.RichTextWebAdapter = void 0;

var _react = _interopRequireDefault(require("react"));

var _check = _interopRequireDefault(require("../../check"));

var _reactHtmlParser = require("@hedgedoc/html-to-react");

var _get = require("../../../utils/get");

var _util = require("./util");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

var RichTextWebAdapter = {
  transform: function transform(node, index) {
    var _this = this;

    if (node.type === "tag") {
      this.idCount += 1;

      if (node.attribs) {
        node.attribs = _objectSpread(_objectSpread({}, node.attribs), {}, {
          transformedStyle: (0, _util.InlineStyleToObject)((0, _get.get)(node.attribs, "style", "")),
          class: "dzq-".concat(node.name, " ").concat((0, _get.get)(node, "attribs.class", "")),
          "data-nodeId": this.idCount,
          originType: node.name
        });
      }

      this.nodes[this.idCount] = node;

      switch (node.name) {
        case "img":
          this.imgUrls.push(node.attribs.src);
          return /*#__PURE__*/_react.default.createElement("img", _extends({}, node.attribs, {
            style: node.attribs.transformedStyle,
            className: node.attribs.class,
            onLoad: function onLoad(e) {
              _this.loadedImgs.push(node.attribs["data-nodeId"]);

              _this.onImgsLoaded(e);
            },
            onError: function onError(error) {
              _this.erroredImgs.push(node.attribs["data-nodeId"]);

              _this.onImgsErrored(error);
            },
            alt: "\u56FE\u7247"
          }));

        case "iframe":
          {
            // 非白名单内的节点不渲染
            if (!this.isValidIframeSrc(node.attribs.src)) {
              return null;
            } // 转换内部属性到 style，支持自定义宽高


            if (!node.attribs.style) {
              node.attribs.style = "";
            }

            if ((0, _get.get)(node, "attribs.width")) {
              if (Number((0, _get.get)(node, "attribs.width")) === NaN) {
                node.attribs.style += "width: ".concat((0, _get.get)(node, "attribs.width"), ";");
              } else {
                node.attribs.style += "width: ".concat((0, _get.get)(node, "attribs.width"), "px;");
              }
            }

            if ((0, _get.get)(node, "attribs.height")) {
              if (Number((0, _get.get)(node, "attribs.height")) === NaN) {
                node.attribs.style += "height: ".concat((0, _get.get)(node, "attribs.height"), ";");
              } else {
                node.attribs.style += "height: ".concat((0, _get.get)(node, "attribs.height"), "px;");
              }
            } // iframe 允许全屏


            node.attribs.allowfullscreen = true;
            break;
          }

        case "input":
          {
            var checkLabelState = (0, _get.get)(node, "parent.attribs.data-task-state", {});
            return /*#__PURE__*/_react.default.createElement(_check.default, {
              checked: checkLabelState === "checked"
            });
          }

        case "a":
          {
            // 通过a标签的过滤
            if ((0, _get.get)(node, 'attribs.href', '').indexOf('javascript:') !== -1) {
              return null;
            }

            break;
          }
      }

      node.attribs.transformedStyle = (0, _get.get)(node.attribs, "style", "");
      return (0, _reactHtmlParser.convertNodeToElement)(node, index, this.adapter.transform);
    }
  },
  onWrapperClick: function onWrapperClick(e) {
    e.stopPropagation();
    this.onClick(e);
    var nodeId = (0, _get.get)(e, "target.dataset.nodeid", "");
    if (!nodeId) return;
    var targetNode = this.nodes[nodeId];

    if (targetNode) {
      var originType = (0, _get.get)(e, "target.nodeName", "");

      if (originType === "IMG") {
        // emoji 表情外才能触发点击事件
        if ((0, _get.get)(targetNode, "attribs.class", "").indexOf("qq-emotion") === -1) {
          this.onImgClick(targetNode, e);
        }
      }

      if (originType === "A") {
        this.onLinkClick(targetNode, e);
      }
    }
  }
};
exports.RichTextWebAdapter = RichTextWebAdapter;
